import random
import math
import time
import sys
import os

objList = ["bike", "ball", "car", "truck", "box"]
objCounts = {}
NUMTYPES = 5

numPeople = random.randint(1,4)
numObjects = random.randint(1,8)

dirName = str(int(time.time()*100))[7:]

os.system("mkdir "+dirName)

people = []
objects = []
vehicles = {"bike" : [], "car" : [], "truck" : []}

#person:
#0 -> id 1 -> x pos 2 -> y pos 3 -> z pos 4 -> x vel 5 -> y vel 6 -> z vel 7 -> attachment type 8 -> attachment id
for i in range(0,numPeople):
	people.append([])
	people[i].append(i)
	people[i].append(random.uniform(-200,200))
	people[i].append(random.uniform(-200,200))
	people[i].append(random.uniform(-200,200))
	people[i].append(random.uniform(-.2,.2))
	people[i].append(random.uniform(-.2,.2))
	people[i].append(random.uniform(-.2,.2))
	people[i].append(".")
	people[i].append(".")

print people

#object:
#0 -> type 1 -> id 2 -> x pos 3 -> y pos 4 -> z pos 5 -> x vel 6 -> y vel 7 -> z vel 8 -> attachment type 9 -> attachment id
for i in range(0,numObjects):
	objects.append([])
	objType = objList[random.randint(0,NUMTYPES-1)]
	if not objType in objCounts:
		objCounts[objType] = 0
	objects[i].append(objType)
	objects[i].append(objCounts[objType])
	objCounts[objType] += 1
	objects[i].append(random.uniform(-200,200))
	objects[i].append(random.uniform(-200,200))
	objects[i].append(random.uniform(-200,200))
	if objType in ["car", "truck"]:
		objects[i].append(random.uniform(-2,2))
		objects[i].append(random.uniform(-2,2))
		objects[i].append(random.uniform(-2,2))
		objects[i].append(".")
		objects[i].append(".")
	elif objType == "bike":
		objects[i].append(random.uniform(-.8,.8))
		objects[i].append(random.uniform(-.8,.8))
		objects[i].append(random.uniform(-.8,.8))
		objects[i].append(".")
		objects[i].append(".")
	else:
		objects[i].append(random.uniform(-.2,.2))
		objects[i].append(random.uniform(-.2,.2))
		objects[i].append(random.uniform(-.2,.2))
		if random.randint(1,3) == 1:
			objects[i].append("person")
			objects[i].append(random.randint(0,numPeople-1))
		else:
			objects[i].append(".")
			objects[i].append(".")
	if objType == "bike":
		vehicles["bike"].append(objects[i])
	if objType == "car":
		vehicles["car"].append(objects[i])
	if objType == "truck":
		vehicles["truck"].append(objects[i])

numBikes = objCounts["bike"] if "bike" in objCounts else 0
numCars = objCounts["car"] if "car" in objCounts else 0
numTrucks = objCounts["truck"] if "truck" in objCounts else 0
totalVehicles = numCars + numTrucks
peopleOnBikes = 0
if (numBikes+totalVehicles) > 0:
	for person in people:
		if (numBikes-peopleOnBikes+totalVehicles) == 0:
			break
		if random.randint(1,2) == 1:
			vehicleNum = random.randint(1,(numBikes-peopleOnBikes+totalVehicles))
			if vehicleNum <= numBikes-peopleOnBikes:
				person[7] = "bike"
				person[8] = vehicleNum-1
				peopleOnBikes+=1
			else:
				vehicleNum -= (numBikes-peopleOnBikes)
				if vehicleNum <= numCars:
					person[7] = "car"
					person[8] = vehicleNum-1
				else:
					vehicleNum -= numCars
					person[7] = "truck"
					person[8] = vehicleNum-1

print objects

for i in range(0,401):
	fileName = dirName+"/frame{0:05d}.txt".format(i)
	outFile = open(fileName,"w")
	for person in people:
		if person[7] == ".":
			person[1]+=person[4]
			person[2]+=person[5]
			person[3]+=person[6]
		else:
			attachedVehicle = vehicles[person[7]][person[8]]
			person[1] = attachedVehicle[2]+attachedVehicle[5]
			person[2] = attachedVehicle[3]+attachedVehicle[6]
			person[3] = attachedVehicle[4]+attachedVehicle[7]
		if random.randint(1,5) == 1:	
			outFile.write("person\t"+str(person[0])+"\t{0:.2f}\t{1:.2f}\t{2:.2f}\t{3}\t{4}\n".format(person[1]+random.uniform(-.25,.25),person[2]+random.uniform(-.25,.25),person[3]+random.uniform(-.25,.25),person[7],person[8]))
		else:
			outFile.write("person\t"+str(person[0])+"\t{0:.2f}\t{1:.2f}\t{2:.2f}\t{3}\t{4}\n".format(person[1]+random.uniform(-.25,.25),person[2]+random.uniform(-.25,.25),person[3]+random.uniform(-.25,.25),".","."))
		#print "person\t"+str(person[0])+"\t{0:.2f}\t{1:.2f}\t{2:.2f}\t{3}\t{4}".format(person[1],person[2],person[3],person[7],person[8])
	for obj in objects:
		if obj[8] == ".":
			obj[2] += obj[5]
			obj[3] += obj[6]
			obj[4] += obj[7]
		else:
			attachedPerson = people[obj[9]]
			obj[2] = attachedPerson[1]
			obj[3] = attachedPerson[2]
			obj[4] = attachedPerson[3]
		if random.randint(1,5) == 1:
			outFile.write(obj[0]+"\t"+str(obj[1])+"\t{0:.2f}\t{1:.2f}\t{2:.2f}\t{3}\t{4}\n".format(obj[2]+random.uniform(-.25,.25),obj[3]+random.uniform(-.25,.25),obj[4]+random.uniform(-.25,.25),obj[8],obj[9]))
		else:
			outFile.write(obj[0]+"\t"+str(obj[1])+"\t{0:.2f}\t{1:.2f}\t{2:.2f}\t{3}\t{4}\n".format(obj[2]+random.uniform(-.25,.25),obj[3]+random.uniform(-.25,.25),obj[4]+random.uniform(-.25,.25),".","."))
		#print obj[0]+"\t"+str(obj[1])+"\t{0:.2f}\t{1:.2f}\t{2:.2f}\t{3}\t{4}".format(obj[2],obj[3],obj[4],obj[8],obj[9])
	outFile.close()
